import {getRequest} from './api'
import {Message} from 'element-ui'

export const isNotNullORBlank = (...args)=> {
  for (var i = 0; i < args.length; i++) {
    var argument = args[i];
    if (argument == null || argument == '' || argument == undefined) {
      Message.warning({message: '数据不能为空!'})
      return false;
    }
  }
  return true;
}
export const initMenu = (router, store)=> {
  //首先判断store中的数据是否存在，如果存在，说明这次跳转是正常的跳转，而不是用户按F5或者直接在地址栏输入某个地址进入的。
  console.log(store.state.user.username);
  if (store.state.routes.length > 0) {
    console.log('路由不为空');
    return;
  }
  //发送请求获取用户的权限
  getRequest("/permission/info").then(data=> {
      var fmtRoutes = formatRoutes(data);
      router.addRoutes(fmtRoutes);
      store.commit('initMenu', fmtRoutes);
  })
}
//服务器返回的json转为router需要的格式
export const formatRoutes = (routes)=> {
  let fmRoutes = [];
  routes.forEach(router=> {
    let {
      path,
      component,
      name,
      children
    } = router;
    if (null != children) {
      children = formatRoutes(children);
    }
    let fmRouter = {
      path: path,
      component(resolve){
        // if (component.startsWith("Home")) {
        //   require(['../components/' + component + '.vue'], resolve)
        // } else if (component.startsWith("Emp")) {
        //   require(['../components/permission/' + component + '.vue'], resolve)
        // } 
        if (path == '/') {
          require(['../components/' + component + '.vue'], resolve)
        } else {
          require(['../components' + path + '/' + component + '.vue'], resolve);
        } 
        
      },
      name: name,
      children: children
    };
    fmRoutes.push(fmRouter);
  })
  return fmRoutes;
}
